<?php

class Nuo_Validator_Url extends Nuo_Validator_Abstract
{
	/**
	 * @var array
	 */
	public $_config = array('msg' => 'Invalid Url');

	/**
	 * @var string
	 */
	public $pattern = '/^{schemes}:\/\/(([A-Z0-9][A-Z0-9_-]*)(\.[A-Z0-9][A-Z0-9_-]*)+)/i';

	/**
	 * @var array
	 */
	public $validSchemes = array(
		'http',
		'https'
	);

	/**
	 * @var string
	 */
	public $defaultScheme;

	/**
	 * @param $mixed $value value to test
	 * @param array $params
	 * @return bool
	 */
	public function isValid($value, $params = null)
	{
		if (is_string($value) && strlen($value) < 2000)// make sure the length is limited
		// to avoid DOS attacks
		{
			if ($this -> defaultScheme !== null && strpos($value, '://') === false)
				$value = $this -> defaultScheme . '://' . $value;

			if (strpos($this -> pattern, '{schemes}') !== FALSE)
			{

				$pattern = str_replace('{schemes}', '(' . implode('|', $this -> validSchemes) . ')', $this -> pattern);
			}
			else
			{

				$pattern = $this -> pattern;
			}

			if (preg_match($pattern, $value))
			{

				return $value;
			}
		}
		return FALSE;
	}

}
